package com.qiusuo0226.platform.handler;

import com.qiusuo0226.mvc.util.JsonResultModel;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * Error处理解密
 * SpringBoot 发生 404 后会重定向到 /error 页面，所以我们定义访问 /error 路径，返回一个 JSON 对象
 * @author qiusuo
 * @date 2023/5/12 14:38
 */
@Controller
public class ErrorHandler implements ErrorController {

    @RequestMapping(value = "", produces = "text/html;charset=UTF-8")
    public String handleHtmlError() {
//        return new RedirectView("error/404.html");
        return "views/error/404";
    }

    @RequestMapping(value = "/error")
    @ResponseBody
    public JsonResultModel handleJsonError(HttpServletResponse response) {
        int status = response.getStatus();
        return JsonResultModel.failure(String.valueOf(status), null);
    }
}
